package cn.syncrond.tcm.core.definition;

import java.util.ArrayList;
import java.util.List;

import cn.syncrond.tcm.core.ext.ITaskEventListener;
import cn.syncrond.tcm.core.ext.ITaskEventListener.ITaskEndEventListener;
import cn.syncrond.tcm.core.ext.ITaskEventListener.ITaskErrorEventListener;
import cn.syncrond.tcm.core.ext.ITaskEventListener.ITaskPauseEventListener;
import cn.syncrond.tcm.core.ext.ITaskEventListener.ITaskResumeEventListener;
import cn.syncrond.tcm.core.ext.ITaskEventListener.ITaskStartEventListener;
import cn.syncrond.tcm.core.ext.IVisitor;
import cn.syncrond.tcm.core.ext.TaskEvent;
import cn.syncrond.tcm.core.pool.PauseSupport;

/**
 * @Description: 抽象的任务基类
 * @author meepai
 * @date 2016-11-21
 */
public abstract class AbstractTask extends PauseSupport implements ITask {

	private static final long serialVersionUID = 4354672019553165033L;
	
	/**
	 * 任务事件监听器列表
	 */
	protected List<ITaskEventListener> listeners = new ArrayList<ITaskEventListener>();
	
	/**
	 * 任务开始时间
	 */
	protected Long startTime;
	
	/**
	 * 任务结束事件
	 */
	protected Long endTime;
	
	/**
	 * 任务状态
	 */
	protected TaskExecutionStatus executionStatus;
	
	public AbstractTask() {
		// 默认为未解析状态
		executionStatus = TaskExecutionStatus.UNRESOLVED;
	}
	
	@Override
	public void pause() {
		super.pause();
		this.fireTaskPauseEvent();
		// 设置暂停状态
		this.setExecutionStatus(TaskExecutionStatus.PAUSED);
	}
	
	@Override
	public void resume() {
		super.resume();
		this.fireTaskResumeEvent();
		// 设置运行状态
		this.setExecutionStatus(TaskExecutionStatus.RUNNING);
	}
	
	@Override
	public void accept(IVisitor visitor) {
		visitor.visit(this);
	}

	@Override
	public Long getStartTime() {
		return startTime;
	}

	@Override
	public void setStartTime(Long time) {
		this.startTime = time;
	}
	
	@Override
	public Long getEndTime() {
		return endTime;
	}

	@Override
	public void setEndTime(Long endTime) {
		this.endTime = endTime;
	}
	
	@Override
	public void setExecutionStatus(TaskExecutionStatus executionStatus) {
		this.executionStatus = executionStatus;
	}
	
	@Override
	public void addTaskEventListener(ITaskEventListener listener) {
		listeners.add(listener);
	}

	@Override
	public void fireTaskStartEvent() {
		ITaskEventListener listener;
		for(int i=0, n=listeners.size(); i<n; i++){
			listener = listeners.get(i);
			if(listener instanceof ITaskStartEventListener){
				listener.handleEvent(new TaskEvent(this));
			}
		}
	}
	
	@Override
	public void fireTaskEndEvent() {
		ITaskEventListener listener;
		for(int i=0, n=listeners.size(); i<n; i++){
			listener = listeners.get(i);
			if(listener instanceof ITaskEndEventListener){
				listener.handleEvent(new TaskEvent(this));
			}
		}
	}
	
	@Override
	public void fireTaskErrorEvent() {
		ITaskEventListener listener;
		for(int i=0, n=listeners.size(); i<n; i++){
			listener = listeners.get(i);
			if(listener instanceof ITaskErrorEventListener){
				listener.handleEvent(new TaskEvent(this));
			}
		}
	}
	
	@Override
	public void fireTaskPauseEvent() {
		ITaskEventListener listener;
		for(int i=0, n=listeners.size(); i<n; i++){
			listener = listeners.get(i);
			if(listener instanceof ITaskPauseEventListener){
				listener.handleEvent(new TaskEvent(this));
			}
		}
	}
	
	@Override
	public void fireTaskResumeEvent() {
		ITaskEventListener listener;
		for(int i=0, n=listeners.size(); i<n; i++){
			listener = listeners.get(i);
			if(listener instanceof ITaskResumeEventListener){
				listener.handleEvent(new TaskEvent(this));
			}
		}
	}
	
}
